package com.baomibing.flow.enginee;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomibing.flow.constant.ProcessTypeEnum;
import com.baomibing.flow.execute.Execution;
import com.baomibing.flow.model.WorkFlow;
import com.baomibing.flow.model.WorkNode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

/**
 * ParallelProcess
 *
 * @author frog 2023/9/20 17:12
 * @version 1.0.0
 **/

@Getter @ToString
public class ParallelProcess extends Process {

    @JSONField(deserialize=false)
    @Setter
    private List<Process> processes;

    @JSONField(deserialize=false)
    private WorkNode source;

    public ParallelProcess(WorkFlow flow) {
        super(flow);
        this.processType = ProcessTypeEnum.PARALLEL.name();
    }

    @Override
    public boolean beComplete() {
        for (Process p : processes) {
            if (!p.beComplete()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean doProcess(Execution execution) {
        for (Process p : processes) {
            if (p.beComplete()) {
                continue;
            }
            if (!p.doProcess(execution)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean doReject(Execution execution) {
        for (Process p : processes) {
            if (!p.doReject(execution)) {
                return false;
            }
        }
        return true;
    }

    public ParallelProcess source(WorkNode source) {
        this.source = source;
        this.source.setFlow(flow);
        return this;
    }
}
